
	<script type="text/javascript">

	function validatePassword(pwd){
    var reg = /^[a-zA-Z0-9]{8,30}$/;
    
    var resultValid = new Array();
    
    if(pwd.match(reg) != null){
      reg = /[a-z]/;  
      resultValid[0] = reg.test(pwd);
      reg = /[A-Z]/;  
      resultValid[1] = reg.test(pwd);
      reg = /[0-9]/;  
      resultValid[2] = reg.test(pwd);
      
      if(resultValid[0] == false || resultValid[1] == false || resultValid[2] == false){
        return null;
      }else{
        return true;
      }
    }
    return null;
  }

	$(function(){

		var passwordIsCorrect = true;
		var cormfirmPassIsCorrect = true;
		$('#newPass').bind('keyup',function(){
      if(validatePassword($('#newPass').val()) == null){
        $('#newPass').parent().parent().removeAttr('class');
        $('#newPass').parent().parent().addClass('control-group error');
        $('#newPass').next().html('รหัสผ่านใหม่ไม่ถูกต้อง');
        passwordIsCorrect = true;
      }else{
        $('#newPass').parent().parent().removeAttr('class');
        $('#newPass').parent().parent().addClass('control-group success');
        $('#newPass').next().html('');
        passwordIsCorrect = true;
      }
    });
    
    $('#confirmPass').bind('keyup',function(){
      if($('#confirmPass').val() == $('#newPass').val()){
        $('#confirmPass').parent().parent().removeAttr('class');
        $('#confirmPass').parent().parent().addClass('control-group success');
        $('#confirmPass').next().html('');
        cormfirmPassIsCorrect = true;
      }else{
        $('#confirmPass').parent().parent().removeAttr('class');
        $('#confirmPass').parent().parent().addClass('control-group error');
        $('#confirmPass').next().html('การยืนยันรหัสผ่านไม่ถูกต้อง');
        cormfirmPassIsCorrect = false;
      }
    });
		//alert('as');
		$('#btnSubmit').bind('click', function() {
		  //alert('a');

		  var haveBlank = false;
		  $('form').find('input').each(function(){
				//Check Null
				if($(this).val() == ""){
					$(this).parent().parent().addClass('warning');
					var nameInput = $(this).parent().prev().html();
					//alert(nameInput);
					$(this).next().html('กรุณาใส่ '+nameInput);
					haveBlank = true;
				}else{
					$(this).parent().parent().removeClass('warning');
					$(this).next().html('');
				}

		  });
		  //if see blank stop working loop
		  if(haveBlank){
		  	return;
		  }

		  if(passwordIsCorrect == false || cormfirmPassIsCorrect == false){
		  	return;
		  }

		  $("#notify").removeAttr('class');
		  //check newpass equal confirm password
		  if( $.trim($('#newPass').val()) != $.trim($('#confirmPass').val()) ){
		  	$('#notify').attr('class','alert alert-error');
				$('#notify strong').html('เกิดข้อผิดพลาด!');
				$('#notify span').html('การยืนยันรหัสผ่านไม่ถูกต้อง');
				$('#notify').show();
				return;
		  }else{
				$('#notify').removeClass('alert-error');
				$('#notify').hide();
		  }

		  //check old password
			$.ajax({
				type : 'POST',
				url : 'checkOldPassword',
				data : { oldPass : $('#oldPass').val() },
				success:function(result){
					//alert(result);
					if(result.indexOf('TRUE') > -1){
						$.post("updateNewPassword", { newPass : $('#newPass').val() } );

						$('form').find('input').each(function(){
							$(this).val('');
						});

						$('#notify').attr('class','alert alert-success');
						$('#notify strong').html('สำเร็จ!');
						$('#notify span').html('ระบบเปลี่ยนรหัสผ่านเรียบร้อยแล้ว');
						$('#notify').show();

						$('#newPass').parent().parent().removeClass('success');
						$('#confirmPass').parent().parent().removeClass('success');
					}else{
						$('#notify').attr('class','alert alert-error');
						$('#notify strong').html('เกิดข้อผิดพลาด!');
						$('#notify span').html('รหัสผ่านไม่ถูกต้อง');
						$('#notify').show();
					}
				},
				error:function(err){
					alert('error '+err);
				}
			});

		});

		
	});
	</script>

	<section id="home">

		<ul class="breadcrumb">
		  <li><a href="<?php echo base_url('index.php/mainController/'); ?>">Home</a> <span class="divider">/</span></li>
		  <li class="active">เปลี่ยนรหัสผ่าน</li>
		</ul>


	<div class="page-header">
	  <h1>เปลี่ยนรหัสผ่าน</h1>
	</div>

	<div id="notify" class="alert alert-error" style="display:none;">
		<a class="close" onclick="$('.alert').hide()">&times;</a>  
	  <strong></strong> <span></span>
	</div>

	<form id="frmChangePass" class="form-horizontal">
		<div class="control-group">
			<label class="control-label" for="oldPass">รหัสผ่านเก่า</label>
			<div class="controls">
			  <input type="password" name="oldPass" id="oldPass" placeholder="รหัสผ่านเก่า" value="">
			  <span class="help-inline"></span>
			</div>
		  </div>
		  <div class="control-group">
			<label class="control-label" for="newPass">รหัสผ่านใหม่</label>
			<div class="controls">
			  <input type="password" name="newPass" id="newPass" placeholder="รหัสผ่านใหม่" value="">
			  <span class="help-inline"></span>
			</div>
		  </div>
		  <div class="control-group">
			<label class="control-label" for="confirmPass">ยืนยันรหัสผ่าน</label>
			<div class="controls">
			  <input type="password" name="confirmPass" id="confirmPass" placeholder="ยืนยันรหัสผ่าน" value="">
			  <span class="help-inline"></span>
			</div>
		  </div>
		  <div class="control-group">
			<div class="controls">
				<button id="btnClear" type="reset" class="btn">ล้างค่า</button>

				<button id="btnSubmit" type="button" class="btn btn-primary" data-loading-text="Loading...">ตกลง</button>
				<span class="help-inline"></span>
			</div>
		  </div>
		</form>



	 </section>
